﻿using System;
using System.Web;
using System.Text;
using System.Data;
using System.Xml;
using System.Web.SessionState;
using System.Web.Services;

using Gift.DAL;
using Gift.Model;
using Gift.WebApp;

namespace WebApp.cn.ashx
{
    public class addShoppingCartHandler : IHttpHandler, IRequiresSessionState
    {
        /// <summary>
        /// 您将需要在您网站的 web.config 文件中配置此处理程序，
        /// 并向 IIS 注册此处理程序，然后才能进行使用。有关详细信息，
        /// 请参见下面的链接: http://go.microsoft.com/?linkid=8101007
        /// </summary>
        #region IHttpHandler Members

        // Fields
        BasePage thePage = new BasePage();
        tbShoppingCart oShoppingCart = new tbShoppingCart();

        public bool IsReusable
        {
            // 如果无法为其他请求重用托管处理程序，则返回 false。
            // 如果按请求保留某些状态信息，则通常这将为 false。
            get { return true; }
        }

        public void ProcessRequest(HttpContext context)
        {
            try
            {
                if (thePage.LoginUserMember == null)
                {
                    this.oShoppingCart.user = new ClinetMAC().GetClinetMAC();
                    this.oShoppingCart.userType = false;
                    if (HttpContext.Current.Request.Cookies["shopping_cart_mac"] == null)
                    {
                        HttpCookie cookie = new HttpCookie("shopping_cart_mac");
                        cookie.Value = this.oShoppingCart.user;
                        cookie.Expires = DateTime.Now.AddDays(30.0);
                        cookie.Secure = false;
                        HttpContext.Current.Response.Cookies.Add(cookie);
                    }
                }
                else
                {
                    this.oShoppingCart.user = thePage.LoginUserMember.MemberId;
                    this.oShoppingCart.userType = true;
                }
                this.oShoppingCart.goodsEntitys = Convert.ToInt32(context.Request.Form["goodsEntitys"]);
                this.oShoppingCart.amount = Convert.ToInt32(context.Request.Form["count"]);
                if (BaseFactory.theShoppingCart.AddShoppingCart(oShoppingCart) > 0)
                {
                    context.Response.Write("1");
                }
                else
                {
                    context.Response.Write("0");
                }
            }
            catch
            {
                context.Response.Write("1");
            }
        }

        #endregion
    }
}
